﻿using System;
using System.Xml;
using System.Xml.Linq;
using System.IO;

using TJDevHouse.Starcraft.Game;


namespace TJDevHouse.Starcraft.AIModuleConnector.ImportExport
{
	/// <summary>
	/// Handles the storage and retrieval of map data
	/// </summary>
	public static class MapDataStorageController
	{
		/// <summary>
		/// Calculate the name of the directory to store the files for the supplied
		/// map in.
		/// </summary>
		/// <param name="map"></param>
		/// <returns></returns>
		public static string GetMapDataDirName(Map map)
		{
			System.IO.FileInfo exe = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
			
			
			return exe.DirectoryName + "\\maps\\" + map.MapHash.ToString() + " (" + map.MapName + ")";
		}

		public static string GetMapDataFileName(Map map, string analysisName)
		{
			return analysisName + ".txt";
		}

		public static string GetFullMapDataFileName(Map map)
		{
			return GetFullMapDataFileName(map, "mapData");
		}

		public static string GetFullMapDataFileName(Map map, string analysisName)
		{
			return GetMapDataDirName(map) + "\\" + GetMapDataFileName(map, analysisName);
		}

		/// <summary>
		/// Returns a string reader to the specified map analysis data.
		/// 
		/// If the requested data does not exist, will throw an exception.
		/// </summary>
		/// <param name="map"></param>
		/// <param name="analysisName"></param>
		/// <returns></returns>
		public static StreamReader GetMapAnalysisDataReader(Map map, string analysisName)
		{
			return GetMapAnalysisDataReader(GetFullMapDataFileName(map, analysisName));
		}

		public static StreamReader GetMapAnalysisDataReader(string analysisFileName)
		{
			StreamReader reader = new StreamReader(analysisFileName);
			return reader;
		}

		/// <summary>
		/// Get a stream writer used to write data to the specified map data file.
		/// </summary>
		/// <param name="map"></param>
		/// <param name="analysisName"></param>
		/// <returns></returns>
		public static StreamWriter GetMapAnalysisDataWriter(Map map, string analysisName)
		{
			StreamWriter writer = new StreamWriter(GetFullMapDataFileName(map, analysisName));
			return writer;
		}



		/// <summary>
		/// Saves the map data in compact format
		/// </summary>
		/// <param name="map"></param>
		/// <returns></returns>
		public static void SaveCompactMapData(Map map)
		{
			GenerateCompactMapData(map);
		}


		public static void GenerateCompactMapData(Map map)
		{
			//create the directory if needed
			Directory.CreateDirectory(GetMapDataDirName(map));
			StreamWriter writer = GetMapAnalysisDataWriter(map, "mapData");
			CompactMapDataImportExporter generator = new CompactMapDataImportExporter();
			generator.MakeExportMapData(map, writer);
			writer.Close();
		}

		/// <summary>
		/// Whether or not the supplied map has its map data saved
		/// or not
		/// </summary>
		/// <param name="map"></param>
		/// <returns></returns>
		public static bool MapDataIsSaved(Map map)
		{
			return MapDataIsSaved(map, "mapData");
		}


		public static bool MapDataIsSaved(Map map, string analysisName)
		{
			if (!Directory.Exists(GetMapDataDirName(map)))
			{
				return false;
			}

			if (!File.Exists(GetMapDataDirName(map) + "\\" + GetMapDataFileName(map, analysisName)))
			{
				return false;
			}

			return true;
		}
		

		/// <summary>
		/// Creates the map specified by the unique map hash from saved data.
		/// </summary>
		/// <param name="MapHash"></param>
		/// <returns></returns>
		public static Map CreateMap(int mapHash)
		{
			throw new NotImplementedException("CreateMap() not implemented yet...");
		}

		/// <summary>
		/// Creates a map by specifying the path to its mapData XML file
		/// </summary>
		/// <param name="mapDataFilePath"></param>
		/// <returns></returns>
		public static Map CreateMap(string mapDataFilePath)
		{
			CompactMapDataImportExporter importer = new CompactMapDataImportExporter();

			
			StreamReader reader = new StreamReader(mapDataFilePath);
			return importer.MakeMapFromMapData(reader);
		}


	}
}
